package com.sqx.modules.indentTimeRecord.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sqx.modules.errand.entity.Indent;
import com.sqx.modules.errand.service.IndentService;
import com.sqx.modules.indentTimeRecord.entity.IndentTimeRecord;
import com.sqx.modules.indentTimeRecord.dao.IndentTimeRecordDao;
import com.sqx.modules.indentTimeRecord.service.IndentTimeRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.modules.packageTime.entity.PackageTime;
import com.sqx.modules.packageTime.service.PackageTimeService;
import org.springframework.stereotype.Service;
import com.sqx.common.utils.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * 加时记录 服务实现类
 * @author wuChang
 * @since 2024-05-22
 */
@Service
public class IndentTimeRecordServiceImp extends ServiceImpl<IndentTimeRecordDao, IndentTimeRecord> implements IndentTimeRecordService {
    @Autowired
    private IndentService indentService;
    @Autowired
    private PackageTimeService packageTimeService;

    @Override
    public Result saveIndentTimeRecord(IndentTimeRecord indentTimeRecord) {
        if (indentTimeRecord.getRecordId() != null) {
            return Result.upStatus(baseMapper.updateById(indentTimeRecord));
        } else {
            indentTimeRecord.setCreateTime(LocalDateTime.now());
            return Result.upStatus(baseMapper.insert(indentTimeRecord));
        }
    }

    @Override
    public IPage<IndentTimeRecord> getIndentTimeRecordList(Integer page, Integer limit, IndentTimeRecord indentTimeRecord) {
        Page<IndentTimeRecord> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }
        return baseMapper.selectPage(pages, new QueryWrapper<>(indentTimeRecord).orderByAsc("create_time"));
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result indentAddTime(Long userId, Long indentId, Long timeId) {

        Indent indent = indentService.getById(indentId);
        if (indent == null) {
            return Result.error("订单不存在");
        }
        if (userId != null) {
            if (!userId.equals(indent.getRiderUserId())) {
                return Result.error("你无权操作");
            }
        }
        if (indent.getIndentState() != 2) {
            return Result.error("当前状态不可加时");
        }
        PackageTime packageTime = packageTimeService.getById(timeId);
        if (packageTime == null) {
            return Result.error("时间配置不存在");
        }
        if (packageTime.getIsEnable() != 1) {
            return Result.error("当前时间配置不存在,请选择其他时间");
        }
        Integer time;
        if (packageTime.getTimeUnit() == 1) {
            time = packageTime.getTimeNum();
        } else if (packageTime.getTimeUnit() == 2) {
            time = packageTime.getTimeNum() * 60;
        } else if (packageTime.getTimeUnit() == 3) {
            time = packageTime.getTimeNum() * 60 * 24;
        } else {
            return Result.error("时间配置参数异常,请联系管理员");
        }

        indent.setTimeNum(indent.getTimeNum() + time);

        indent.setEstimateMoney(indent.getEstimateMoney().add(packageTime.getMoney()));
        indent.setIndentBasicsMoney(indent.getIndentBasicsMoney().add(packageTime.getMoney()));
        indent.setIndentMoney(indent.getEstimateMoney().add(packageTime.getMoney()));

        IndentTimeRecord indentTimeRecord = new IndentTimeRecord();
        indentTimeRecord.setTimeId(timeId);
        indentTimeRecord.setTime(time);
        indentTimeRecord.setIndentId(indentId);
        indentTimeRecord.setCreateTime(LocalDateTime.now());
        baseMapper.insert(indentTimeRecord);
        return indentService.updateById(indent) ? Result.success() : Result.error();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result indentRemoveTime(Long recordId) {
        IndentTimeRecord indentTimeRecord = baseMapper.selectById(recordId);
        if (indentTimeRecord == null) {
            return Result.error("加时记录不存在");
        }
        Indent indent = indentService.getById(indentTimeRecord.getIndentId());
        if (indent != null) {
            indent.setTimeNum(indent.getTimeNum() - indentTimeRecord.getTime());
            PackageTime packageTime = packageTimeService.getById(indentTimeRecord.getTimeId());
            indent.setEstimateMoney(indent.getEstimateMoney().subtract(packageTime.getMoney()));
            indent.setIndentBasicsMoney(indent.getIndentBasicsMoney().subtract(packageTime.getMoney()));
            indent.setIndentMoney(indent.getIndentMoney().subtract(packageTime.getMoney()));
            indentService.updateById(indent);
        }
        return Result.upStatus(baseMapper.deleteById(recordId));

    }
}
